﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Halk.Analyser;
using Halk.Config;
using Halk.Dao;
using Halk.Model;

namespace Halk.Spider
{
    public class SmartMatch
    {

        private static SmartMatch _smartMatch;

        public static SmartMatch Instace()
        {
            return _smartMatch ?? (_smartMatch = new SmartMatch());
        }

        private readonly Dao.DaoSysMatchs _daoSysMatchs = new DaoSysMatchs(Config.ConfigManage.Instace().GetConfig().ConnString);

        public IEnumerable<RuleEntity> GetReserveRules(int articleid, out bool hasBest)
        {
            IList<RuleEntity> result = new List<RuleEntity>();
            hasBest = false;
            var best = "";
            var sysMatch = _daoSysMatchs.GetModel(articleid);
            if (!string.IsNullOrEmpty(sysMatch.webflag))
            {
                result.Add(Config.ConfigManage.Instace().GetReserveRule(sysMatch.webflag));
                best = sysMatch.webflag;
                hasBest = true;
            }
            foreach (var dicReserveRule in Config.ConfigManage.Instace().GetReserveRuleList())
            {
                if (dicReserveRule.Key != best)
                {
                    result.Add(dicReserveRule.Value);
                }
            }
            return result;
        }

        private string ReplaceChapterName( string chapterName)
        {
            var result = chapterName;
            foreach (var c in Config.ContentManage.Instace().GetSmartReplaceString())
            {
                if (chapterName.Contains(c.Key))
                {
                    result = result.Replace(c.Key, c.Value);
                }
            }
            return result;
        }

        private class ChapterType
        {
            public string Chaptername { get; set; }
            public string Volumename { get; set; }
            public int ChapterID { get; set; }
        }

        public bool MatchChapters(IEnumerable<ChapterInfo> sourceBookReader, 
            IEnumerable<ChapterInfo> reserveBookReader, 
            ref int difference,
            ref Dictionary<int, int> relations)
        {
            bool result = false;
            var sourceChapters = new List<ChapterType>();
            var reserveChapters = new List<ChapterType>();
            foreach (ChapterInfo chapterInfo in sourceBookReader)
            {
                if (chapterInfo.chaptertype != 1) //过滤掉卷内容
                {
                    var chapter = new ChapterType
                                      {
                                          Chaptername = ReplaceChapterName(chapterInfo.chaptername),
                                          Volumename = ReplaceChapterName(chapterInfo.volumename),
                                          ChapterID=chapterInfo.chapterid
                                      };
                    sourceChapters.Add(chapter);
                }
            }

            foreach (ChapterInfo chapterInfo in reserveBookReader)
            {
                if (chapterInfo.chaptertype != 1) //过滤掉卷内容
                {
                    var chapter = new ChapterType
                                      {
                                          Chaptername = ReplaceChapterName(chapterInfo.chaptername),
                                          Volumename = ReplaceChapterName(chapterInfo.volumename),
                                          ChapterID = chapterInfo.chapterid
                                      };
                    reserveChapters.Add(chapter);
                }
            }
            //最新章节名称相同
            //if (MatchNewChapter(sourceChapters[sourceChapters.Count - 1], reserveChapters[reserveChapters.Count - 1]))
            //{
            //    result = true;
            //    //return true;
            //}
            difference = 0;
            for (var i = sourceChapters.Count - 1; i >= 0; i--)
            {
                var items = from item in reserveChapters
                            where
                                ((string.IsNullOrEmpty(item.Volumename) ||
                                  item.Volumename == Config.ContentManage.Instace().Content.DefaultVolumeName) &&
                                 item.Chaptername == sourceChapters[i].Chaptername) ||
                                (item.Volumename == sourceChapters[i].Volumename &&
                                 item.Chaptername == sourceChapters[i].Chaptername)
                            select item;
                if (!items.Any())
                {
                    difference++;
                    Utils.LogManage.Instace().Error(string.Format("[10004]Chapter has not exits{0}", sourceChapters[i].Chaptername));
                }
                else
                {
                    relations[sourceChapters[i].ChapterID] = items.First().ChapterID;
                }
            }
            return result;
        }

        private bool MatchNewChapter(ChapterType source,ChapterType reserve)
        {
            if (string.IsNullOrEmpty(reserve.Volumename) || reserve.Volumename == Config.ContentManage.Instace().Content.DefaultVolumeName)
            {
                return reserve.Chaptername == source.Chaptername;
            }
            else
            {
                return reserve.Volumename == source.Volumename && reserve.Chaptername == source.Chaptername;
            }
        }

        //public bool MatchBookReader(List<ChapterInfo> sourceBookReader, List<ChapterInfo> reserveBookReader, ref int different)
        //{
        //    var sourceChapters = new Dictionary<string, ChapterInfo>();
        //    var reserveChapters = new Dictionary<string, ChapterInfo>();
        //    int index = 1;
        //    foreach (ChapterInfo chapterInfo in sourceBookReader)
        //    {
        //        if (chapterInfo.chaptertype!=1)//过滤掉卷内容
        //        {
        //            chapterInfo.chaptername = ReplaceChapterName(chapterInfo.chaptername);
        //            if (sourceChapters.ContainsKey(chapterInfo.chaptername))//章节名称相同的变更
        //            {
        //                chapterInfo.chaptername += index.ToString(CultureInfo.InvariantCulture);
        //            }
        //            sourceChapters[chapterInfo.chaptername] = chapterInfo;
        //        }
        //    }
        //    foreach (ChapterInfo chapterInfo in reserveBookReader)
        //    {
        //        if (chapterInfo.chaptertype != 1)//过滤掉卷内容
        //        {
        //            chapterInfo.chaptername = ReplaceChapterName(chapterInfo.chaptername);
        //            if (sourceChapters.ContainsKey(chapterInfo.chaptername))
        //            {
        //                chapterInfo.chaptername += index.ToString(CultureInfo.InvariantCulture);
        //            }
        //            reserveChapters[chapterInfo.chaptername] = chapterInfo;
        //        }
        //    }

        //    //?san卷名未处理
        //    //最新章节名称相同
        //    if (reserveBookReader[reserveBookReader.Count - 1].chaptername ==
        //        sourceBookReader[sourceBookReader.Count - 1].chaptername)
        //    {
        //        return true;
        //    }
        //    for (int i = sourceChapters.Count - 1; i >= 0; i--)
        //    {
        //        if (!reserveChapters.ContainsKey(sourceChapters.Keys[i]))
        //            different++;
        //    }
        //}
    }
}
